<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>会员登录</title>
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_1432742_hn9pn86x6r5.css">
    <script src="../js/rem.js"></script>
    <link rel="stylesheet" href="../css/会员登录.css">
    <link rel="stylesheet" href="../css/bottom.css">
    <script src="../js/jquery-1.11.3.min.js"></script>
    <script src="http://at.alicdn.com/t/font_1432742_zjogt54a0il.js"></script>
    <script src="../js/axios.min.js"></script>
    <script src="../js/api.js"></script>
    <script src="../js/会员登录.js"></script>
    <script>
        console.log(a);

        console.log(xg_login_Api);
        console.log(xg_checkuesr_Api);
    </script>
</head>

<body>
    <header>
        <div class="iconfont icon-changyongtubiao-xianxingdaochu-zhuanqu-1" id="goback"></div>
        <h1>会员登录</h1>
    </header>
    <main>
        <div class="regist">
            <ul class="btn">
                <li class="active">手机快捷注册
                    <div class="ba"></div>
                </li>
                <li>账号密码登录
                    <div></div>
                </li>
            </ul>
            <ul class="show">
                <li class="one">
                    <form action="">
                        <div>
                            <input type="text" placeholder="请输入手机号" name="phone" id="phone">
                        </div>
                        <div>
                            <input type="password" placeholder="请输入密码" id="one_pwd">
                        </div>
                        <div>
                            <input type="password" placeholder="请确认密码" id="one_pwdsure">
                        </div>
                        <div>
                            <input type="text" placeholder="请输入验证码" name="verCode" id="yzm">
                            <span class="mark">获取验证码</span>
                        </div>
                        <div class="desc">
                            未注册过的手机将自动注册易果生鲜账户
                        </div>
                        <span id="regist">注册</span>
                        <!-- <input type="submit" value="登录" id="regist"> -->
                    </form>
                </li>

                <li class="two block">
                    <form action="">
                        <div>
                            <input type="text" placeholder="手机号/账号/邮箱" id="two_id">
                        </div>
                        <div>
                            <input type="password" placeholder="请输入密码" id="two_pwd">
                        </div>
                        <div class="desc">
                            忘记密码
                        </div>
                        <span id="two_reg">登录</span>
                    </form>

                </li>
            </ul>
        </div>
        <div class="content2">
            <div class="top">
                <p>其他登录方式</p>
                <div></div>
            </div>
            <div class="qw">
                <div class="qq"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-QQ"></use>
                    </svg>QQ登录</div>
                <div class="wb"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-weibo"></use>
                    </svg>微博登录</div>
            </div>
        </div>
        <div class="bottom">
            <input class="serve" type="checkbox">我已阅读并同意<span>《易果服务协议》</span>和<span>《隐私协议》</span>
        </div>
    </main>
    <script>
        $("#goback").click(function () {
            window.history.back(-1);
        })
        //失去焦点判断账号是否注册
        $('#phone').blur(function () {
            function tipp(tiptxt) {
                var tips = document.querySelector('.tips')
                if (tips) {
                    tips.remove()
                }
                var main = document.querySelector('main')
                var tip = document.createElement('span')
                main.appendChild(tip)
                tip.innerHTML = tiptxt
                tip.classList.add("tips")
                var clear = setTimeout(() => {
                    tip.remove()
                }, 2000);
            }
            let yan1 = /^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9])\d{8}$/
            if (phone.value == '') {
                let tip1 = '请输入手机号'
                tipp(tip1)
                phonefg = false
            } else if (yan1.test(phone.value) == false) {
                let tip = '请输入正确的手机号'
                tipp(tip)
                phonefg = false
            } else {
                phonefg = true
                $.get(xg_checkuesr_Api, {
                    username: phone.value
                }, data => {
                    console.log(data);
                    if (data.code == 0) {
                        let tip = '该用户已注册请登录'
                        tipp(tip)
                    }
                })
            }
        })
        $(function () {
            var main = document.querySelector('main')
            var mark = document.querySelector('.mark')
            var phone = document.querySelector('#phone')

            let phonefg = false;
            //验证码
            let yan = '';
            // 获取验证码
            mark.onclick = function () {
                if (phone.value == "") {
                    let tip = '请输入手机号'
                    tipp(tip)
                    phonefg = false;
                } else {
                    cl()
                }
            }
            let yan1 = /^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9])\d{8}$/

            function cl() {
                let text = phone.value
                if (yan1.test(text) == false) {
                    let tip = '请输入正确的手机号'
                    tipp(tip)
                    phonefg = false;
                } else { //正确手机号
                    console.log(text);
                    djs()
                    $.post(Verification_code_Api, {
                        phonenum: text
                    }, function (data) {

                        console.log(data); //验证码
                        // var timer = setTimeout(() => {
                        var tankuang = document.createElement('div')
                        main.appendChild(tankuang)
                        tankuang.classList.add('tan')
                        tankuang.innerHTML = `您的验证码是:${data.code}`
                        animate(tankuang)
                        // }, 3000);
                        phonefg = true
                        let yan = data.code
                        shuru(yan, data.id)
                    })
                }
            }
            //倒计时
            function djs() {
                var mark = document.querySelector('.mark')
                mark.onclick = null;
                mark.innerHTML = '60秒后重新获取'
                let ti = 60
                var timer = setInterval(() => {
                    ti--
                    mark.innerHTML = `${ti}秒后重新获取`
                    if (ti == 0) {
                        mark.onclick = function () {
                            cl()
                        }
                        mark.innerHTML = '获取验证码'
                        clearInterval(timer)
                    }
                }, 1000);
            }

            //判断验证码
            // shuru()
            // $('#phone').blur(function(){})
            // panduanphone()
            function shuru(yan, id) {
                console.log(yan);
                let phonefg = pwdfg = yamfg = servefg = false
                //点击判断
                //注册
                $('#regist').click(function () {
                    function login() {}
                    //判断手机号
                    panduanphone()
                    //判断手机号
                    function panduanphone() {
                        if (phone.value == '') {
                            let tip1 = '请输入手机号'
                            tipp(tip1)
                            phonefg = false
                        } else if (yan1.test(phone.value) == false) {
                            let tip = '请输入正确的手机号'
                            tipp(tip)
                            phonefg = false
                        } else {
                            phonefg = true
                            password1()
                            $.get(xg_checkuesr_Api, {
                                username: phone.value
                            }, data => {
                                console.log(data);
                                if (data.code == 0) {
                                    let tip = '该用户已注册请登录'
                                    tipp(tip)
                                } else {
                                    login()
                                }
                            })
                        }
                    }
                    //判断密码
                    function password1() {
                        let pwd = /\w{5,17}/
                        if (one_pwd.value == '') {
                            let tip = '请输入密码'
                            tipp(tip)
                            pwdfg = false;
                        } else if (pwd.test(one_pwd.value) == false) {
                            let tip = '请输入至少6位的纯数字'
                            tipp(tip)
                            pwdfg = false;
                        } else if (one_pwd.value != one_pwdsure.value) {
                            let tip = '两次输入的密码不一样'
                            tipp(tip)
                            pwdfg = false;
                        } else {
                            pwdfg = true;
                            yzmsu()
                        }
                    }
                    //输入的验证码
                    function yzmsu() {
                        let yzmv = yzm.value;
                        console.log(yan, yzmv);
                        if (yzmv == "") {
                            let tip = '请输入验证码'
                            tipp(tip)
                            yamfg = false
                        }
                        if (yzmv == yan) {
                            console.log(yan);
                            yamfg = true
                            var serve = document.querySelector('.serve')
                            let b = serve.checked
                            if (b == false) {
                                let tip = '请勾选服务协议'
                                tipp(tip)
                                servefg = false
                            } else {
                                servefg = true
                            }
                        } else {
                            let tip = '验证码错误'
                            tipp(tip)
                            yamfg = false
                        }
                    }
                    //判断
                    if (phonefg == true && pwdfg == true && yamfg == true && servefg == true) {
                        //检测用户是否存在存在,否则提示登录
                        $.get(xg_checkuesr_Api, {
                            username: phone.value
                        }, data => {
                            console.log(data);
                            if (data.code == 0) {
                                let tip = '该用户已注册请登录'
                                tipp(tip)
                            } else {
                                login()
                            }
                        })

                        function login() {
                            console.log(one_pwd.value);
                            let password = Number(one_pwd.value)
                            $.post(update_User_Api, { //上传密码
                                id,
                                password,
                            }, function (data) {
                                console.log(password);
                                console.log(data);
                                if (data == 'sucess') {
                                    let tip = '登录成功跳转中'
                                    tipp(tip)
                                    // 二次注册+++++
                                    $.get(xg_login_Api, {
                                        username: Number(phone.value),
                                        password
                                    }, data => {
                                        console.log(data);
                                    })
                                    // location.href = '../首页.html'
                                }
                            })

                            let jsonstr = localStorage.getItem('user')
                            if (jsonstr) {

                            } else {
                                localStorage.setItem('user', JSON.stringify([{
                                    id: id
                                }]))
                            }

                        }

                        // 传密码
                    }
                })
            }
            // 提示内容
            function tipp(tiptxt) {
                var tips = document.querySelector('.tips')
                if (tips) {
                    tips.remove()
                }
                var tip = document.createElement('span')
                main.appendChild(tip)
                tip.innerHTML = tiptxt
                tip.classList.add("tips")
                var clear = setTimeout(() => {
                    tip.remove()
                }, 2000);
            }

            // 验证码弹框
            function animate(rel) {
                $(".tan").slideDown(200).delay(4000).slideUp(200, function () {
                    $('.tan').remove()
                })
            }






            // 第二种登录方式++++++++++++++++++++++++

            //手机号
            let twotest = /^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9])\d{8}$/
            //邮箱
            let emi = /^\w+[-+.]*\w*@([a-z0-9A-Z\u2E80-\u9FFF]-?)+(\.\w{2,6})+/
            let two_phonefg = two_pwdfg = false;
            let two_emailfg = two_emailpwd = false;
            $("#two_reg").click(function () {
                // login_Api
                let phv = two_id.value; //账号
                let password = two_pwd.value //密码
                //判断邮箱
                let e1 = /@{1,}/
                //是邮箱
                if (e1.test(phv)) {
                    console.log(phv);
                    if (emi.test(phv)) {
                        let email = phv;
                        let password = two_pwd.value
                        //接口验证密码
                        $.post(login_Api, {
                            email,
                            password
                        }, function (data) {
                            console.log(data);
                        })
                        two_phonefg = true;
                    } else {
                        let tip = '请输入正确的邮箱'
                        tipp(tip)
                        two_phonefg = false;
                        console.log();
                    }
                }
                //手机号
                if (twotest.test(phv)) {
                    let phonenum = phv
                    //检测用户是否存在存在,否则提示登录++++++++XG
                    $.get(xg_checkuesr_Api, {
                        username: phonenum
                    }, data => {
                        if (data.code == 1) {
                            let tip = '该手机号未注册'
                            tipp(tip)
                        } else {
                            regist()
                        }
                    })
                    // 登录
                    function regist() {
                        console.log(phone);
                        //账号密码登录功能
                        $.post(login_Api, {
                            phonenum: phv,
                            password
                        }, data => {
                            console.log(data);
                            if (data != -1) {
                                console.log(data);
                                let tip = '登录成功跳转中……'
                                tipp(tip)
                                localStorage.setItem('user', JSON.stringify({
                                    id: data
                                }))
                                location.href = '../首页.html'
                            } else {
                                let tip = '密码错误'
                                tipp(tip)
                            }
                        })
                        two_emailfg = true
                    }
                } else {
                    let tip = '请输入正确手机号'
                    tipp(tip)
                    two_emailfg = false
                }
                if (phv == '') {
                    let tip = '请输入手机号'
                    tipp(tip)
                }
            })
        })
    </script>
</body>

</html>